package com.cxyzy.basic_libs

import android.R
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout


/**
 * 获取通过setContentView设置进去的view
 */
fun getContentView(activity: Activity) = getRootView(activity).getChildAt(0) as ViewGroup

/**
 * 获取activity顶层View
 */
private fun getRootView(activity: Activity) = activity.findViewById<ViewGroup>(R.id.content)

fun addView(view: View, activity: Activity) = addView(view, getRootView(activity))

fun addView(view: View, parentView: ViewGroup): View {
    val layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
    parentView.addView(view, layoutParams)
    return view
}

fun addViewMatchParent(view: View, parentView: ViewGroup): View {
    val layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
    )
    parentView.addView(view, layoutParams)
    return view
}